package com.sxt.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Description: 进行页面跳转的servlet
 * @author: Mr.T
 * @date 2020-11-03 10:32
 */
@WebServlet("/page.do") //  同名的业务  父目录  多层目录
public class PageServlet extends HttpServlet {

    private static final String PAGE_PRE_FIX = "/WEB-INF/view/";

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         *   main.jsp
         *
         *   user.jsp
         *
         *   customer.jsp
         *
         */
        //获取业务标识
        String service = req.getParameter("service");
        try {
            //此时 service 值与方法名相同  则可以根据service找到方法
            Method method = this.getClass().getDeclaredMethod(service,HttpServletRequest.class, HttpServletResponse.class);
            //进行方法调用
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            throw  new RuntimeException("找不到方法:"+service);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转到 WEB-INF下面的view /main.jsp
     * @param req
     * @param resp
     */
    private void main(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PAGE_PRE_FIX+"main.jsp").forward(req,resp);
    }

    /**
     * 去用户列表页面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void userList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PAGE_PRE_FIX+"user/list.jsp").forward(req,resp);
    }

    /**
     * 去客户列表页面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void customerList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PAGE_PRE_FIX+"customer/list.jsp").forward(req,resp);
    }

    /**
     * 去修改密码页面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void updatePwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PAGE_PRE_FIX+"user/updatePwd.jsp").forward(req,resp);
    }

    /**
     * 去修改用户头像页面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void updateImg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PAGE_PRE_FIX+"user/updateImg.jsp").forward(req,resp);
    }
}
